/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "spatial_location_callback_stub.h"

#include <message_parcel.h>
#include "spatial_awareness_common.h"

namespace OHOS {
namespace Msdp {
int SpatialLocationCallbackStub::OnRemoteRequest(uint32_t code,
    MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SPATIAL_LOGI(MODULE_SERVICE, "SpatialLocationCallbackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
        code, option.GetFlags());
    std::u16string descripter = SpatialLocationCallbackStub::GetDescriptor();
    std::u16string remoteDescripter = data.ReadInterfaceToken();
    if (descripter != remoteDescripter) {
        SPATIAL_LOGE(MODULE_SERVICE, "SpatialLocationCallbackStub::OnRemoteRequest failed, descriptor is not matched!");
        return E_GET_SPATIAL_AWARENESS_SERVICE_FAILED;
    }

    switch (code) {
        case static_cast<int>(ISpatialLocationCallback::CMD_GET_DIRECTION): {
            return OnDirectionChangedStub(data);
        }
        case static_cast<int>(ISpatialLocationCallback::CMD_GET_DISTANCE): {
            return OnDistanceChangedStub(data);
        }
        case static_cast<int>(ISpatialLocationCallback::CMD_GET_ANGLE): {
            return OnAngleChangedStub(data);
        }
        case static_cast<int>(ISpatialLocationCallback::CMD_GET_INNEROUTER): {
            return OnInnerOuterChangedStub(data);
        }
        case static_cast<int>(ISpatialLocationCallback::CMD_GET_EXIST): {
            return OnExistChangedStub(data);
        }
        default:
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
    }
    return ERR_OK;
}

int32_t SpatialLocationCallbackStub::OnDirectionChangedStub(MessageParcel& data)
{
    std::unique_ptr<DirectionResponse> directionRes(data.ReadParcelable<DirectionResponse>());
    RETURN_IF_WITH_RET((directionRes == nullptr), E_READ_PARCEL_ERROR);
    OnDirectionChanged(*directionRes);
    return ERR_OK;
}

int32_t SpatialLocationCallbackStub::OnDistanceChangedStub(MessageParcel& data)
{
    std::unique_ptr<DistanceResponse> distanceRes(data.ReadParcelable<DistanceResponse>());
    RETURN_IF_WITH_RET((distanceRes == nullptr), E_READ_PARCEL_ERROR);
    OnDistanceChanged(*distanceRes);
    return ERR_OK;
}

int32_t SpatialLocationCallbackStub::OnAngleChangedStub(MessageParcel& data)
{
    std::unique_ptr<AngleResponse> angleRes(data.ReadParcelable<AngleResponse>());
    RETURN_IF_WITH_RET((angleRes == nullptr), E_READ_PARCEL_ERROR);
    OnAngleChanged(*angleRes);
    return ERR_OK;
}

int32_t SpatialLocationCallbackStub::OnInnerOuterChangedStub(MessageParcel& data)
{
    std::unique_ptr<InnerOuterResponse> ioRes(data.ReadParcelable<InnerOuterResponse>());
    RETURN_IF_WITH_RET((ioRes == nullptr), E_READ_PARCEL_ERROR);
    OnInnerOuterChanged(*ioRes);
    return ERR_OK;
}

int32_t SpatialLocationCallbackStub::OnExistChangedStub(MessageParcel& data)
{
    std::unique_ptr<ExistResponse> existRes(data.ReadParcelable<ExistResponse>());
    RETURN_IF_WITH_RET((existRes == nullptr), E_READ_PARCEL_ERROR);
    OnExistChanged(*existRes);
    return ERR_OK;
}
} // namespace Msdp
} // namespace OHOS